﻿
'This class is used to calculate time differences.
'The calculateTime function returns a string (including minutes when necessary) displaying the time difference ie 1 hour 30 mins ago
'The calculateTimeNoHours function  returns a string (not including minutes) displaying the time difference ie 1 hour ago

Public Class CalculateTime

    Public Shared Function calculateTime(ByVal dt As Date) As String
        Dim dt1 As DateTime = DateTime.Now
        Dim dt2 As DateTime = Convert.ToDateTime(dt)
        Dim ts As TimeSpan = dt1.Subtract(dt2)
        Dim mytimeago As String = ""
        If Convert.ToInt32(ts.TotalDays) <> 0 Then
            If Convert.ToInt32(ts.TotalDays) = 1 Then
                mytimeago = "" & Math.Abs(Convert.ToInt32(ts.TotalDays)) & " Day  ago"
            Else
                mytimeago = "" & Math.Abs(Convert.ToInt32(ts.TotalDays)) & " Days  ago"
            End If

        Else
            If (Convert.ToInt32(ts.TotalMinutes) < 5) AndAlso (Convert.ToInt32(ts.TotalHours) = 0) Then
                mytimeago = "Just  Posted"
            Else
                If (Convert.ToInt32(ts.TotalMinutes) > 5) AndAlso (Convert.ToInt32(Math.Truncate(ts.TotalHours)) = 0) Then
                    mytimeago = Convert.ToInt32(ts.TotalMinutes) Mod 60 & " Mins ago"
                Else
                    If Convert.ToInt32(ts.TotalHours) <> 0 Then
                        If Convert.ToInt32(ts.TotalHours) = 1 Then
                            mytimeago = "" & Convert.ToInt32(ts.TotalHours) & " Hour " & Convert.ToInt32(ts.TotalMinutes) Mod 60 & " Mins ago"
                        Else
                            mytimeago = "" & Convert.ToInt32(ts.TotalHours) & " Hours " & Convert.ToInt32(ts.TotalMinutes) Mod 60 & " Mins ago"
                        End If

                    Else
                        mytimeago = Convert.ToInt32(ts.TotalMinutes) Mod 60 & " Mins ago"
                    End If
                End If
            End If
        End If
        Return mytimeago
    End Function

    Public Shared Function calculateTimeNoHours(ByVal dt As Date) As String
        Dim dt1 As DateTime = DateTime.Now
        Dim dt2 As DateTime = Convert.ToDateTime(dt)
        Dim ts As TimeSpan = dt1.Subtract(dt2)
        Dim mytimeago As String = ""
        If Convert.ToInt32(ts.TotalDays) <> 0 Then
            If Convert.ToInt32(ts.TotalDays) = 1 Then
                mytimeago = "" & Math.Abs(Convert.ToInt32(ts.TotalDays)) & " Day ago "
            Else
                mytimeago = "" & Math.Abs(Convert.ToInt32(ts.TotalDays)) & " Days ago"
            End If
        Else
            If (Convert.ToInt32(ts.TotalMinutes) < 5) AndAlso (Convert.ToInt32(Math.Truncate(ts.TotalHours)) = 0) Then
                mytimeago = "Just Added"
            Else
                If (Convert.ToInt32(ts.TotalMinutes) > 5) AndAlso (Convert.ToInt32(Math.Truncate(ts.TotalHours)) = 0) Then
                    mytimeago = Convert.ToInt32(ts.TotalMinutes) Mod 60 & " Mins ago"
                Else
                    If Convert.ToInt32(ts.TotalHours) <> 0 Then
                        If Convert.ToInt32(ts.TotalHours) = 1 Then
                            mytimeago = "" & Convert.ToInt32(ts.TotalHours) & " Hour ago "
                        Else
                            mytimeago = "" & Convert.ToInt32(ts.TotalHours) & " Hours ago"
                        End If
                    Else
                        mytimeago = Convert.ToInt32(ts.TotalMinutes) Mod 60 & " Mins ago"
                    End If
                End If
            End If
        End If
        Return mytimeago
    End Function
End Class


